home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 24 / MacFormat n. 24 (Spain) / MacFormat 24.bin / Demos / Jungle Activated! / JungleActivated Demo / jung DEMO d / 00042_Memory Management.ls < prev    next >
Encoding:
Text File  |  1996-10-14  |  11.9 KB  |  423 lines

  1. on memoryInitialise
  2.   global gSpinLead, gLastSpinTime, gSpinCount, gMemorySize, gSpinWait, gInitialFree, gOutCasts, gOutCastsCount, gOutCastsHold, gSpinOffset, gSpinMem, gDirectory, gDebug, gFreeCutOff, gCastOrderList
  3.   set gLastSpinTime to ticks()
  4.   set gMemorySize to memorysize()
  5.   set gInitialFree to freeBytes()
  6.   initialiseOutCasts()
  7.   set gOutCastsCount to count(gOutCasts)
  8.   set gOutCastsHold to 1
  9.   calcLead(memorysize() / 1000000)
  10.   set gSpinCount to 1
  11.   set gSpinWait to 1
  12.   if voidp(gSpinOffset) then
  13.     set gSpinOffset to 0
  14.   end if
  15.   set gFreeCutOff to 1000000
  16.   if voidp(gDirectory) then
  17.     set gDirectory to "Jungle Activated! :"
  18.   end if
  19.   setPaths()
  20.   set gDebug to 0
  21.   installf()
  22.   set gCastOrderList to []
  23. end
  24.  
  25. on calcLead mem
  26.   global gSpinLead, gSpinMem
  27.   set gSpinMem to mem * 0.40000000000000002
  28.   set gSpinLead to integer(((mem + 0.20000000000000001) * 1.25) - 5)
  29.   if gSpinLead > 15 then
  30.     set gSpinLead to 15
  31.   end if
  32.   return gSpinLead
  33. end
  34.  
  35. on calcSpinLead mem
  36.   global gSpinLead, gSpinMem
  37.   set gSpinMem to mem
  38.   set gSpinLead to integer(mem / (300000 * 2))
  39.   if gSpinLead > 15 then
  40.     set gSpinLead to 15
  41.   end if
  42. end
  43.  
  44. on preLoadPics
  45.   global gSpinLead, gFreeCutOff, gCastOrderList
  46.   set start to label("Studio")
  47.   if gSpinLead = 15 then
  48.     preLoad(start, start + 29)
  49.   else
  50.     preLoad(start, start + gSpinLead)
  51.     preLoad(start + 30 - gSpinLead, start + 29)
  52.   end if
  53.   put "Memory after spics = " & the freeBytes
  54.   repeat with i = 1 to count(gCastOrderList)
  55.     if freeBytes() > gFreeCutOff then
  56.       preLoadCast(the number of cast getAt(gCastOrderList, i))
  57.     end if
  58.   end repeat
  59.   put "Memory after loads = " & the freeBytes
  60. end
  61.  
  62. on reLoadPics
  63.   global gSpinLead, gSpinOffset
  64.   set middle to frame() - label("Studio")
  65.   set bottom to middle - gSpinLead
  66.   set top to middle + gSpinLead
  67.   set pics to the number of cast "Studio0001"
  68.   set the preLoadEventAbort to 0
  69.   if gSpinLead >= 15 then
  70.     repeat with i = 0 to 29
  71.       if the loaded of cast (pics + i) = 0 then
  72.         preLoadCast(pics + i)
  73.       end if
  74.     end repeat
  75.   else
  76.     if (bottom >= 0) and (top <= 29) then
  77.       repeat with i = 0 to 29
  78.         if (i >= bottom) and (i <= top) then
  79.           if the loaded of cast (pics + i) = 0 then
  80.             preLoadCast(pics + i)
  81.           end if
  82.           next repeat
  83.         end if
  84.         unLoadCast(pics + i)
  85.       end repeat
  86.     else
  87.       if bottom < 0 then
  88.         set bottom to bottom + 30
  89.       else
  90.         set top to top - 30
  91.       end if
  92.       repeat with i = 0 to 29
  93.         if (i >= bottom) or (i <= top) then
  94.           if the loaded of cast (pics + i) = 0 then
  95.             preLoadCast(pics + i)
  96.           end if
  97.           next repeat
  98.         end if
  99.         unLoadCast(pics + i)
  100.       end repeat
  101.     end if
  102.   end if
  103.   set gSpinOffset to 0
  104.   set the preLoadEventAbort to 1
  105. end
  106.  
  107. on showSpics
  108.   global gSpinLead, gSpinOffset
  109.   set hold to EMPTY
  110.   set count to 0
  111.   repeat with i = 1 to 30
  112.     if the loaded of cast (the number of cast "Studio0001" - 1 + i) = 1 then
  113.       set hold to hold & " " & string(i)
  114.       set count to count + 1
  115.     end if
  116.   end repeat
  117.   put hold
  118.   put "Total = " & string(count) & "  Lead = " & string(gSpinLead) & "  Offset = " & string(gSpinOffset) & "  curr = " & frame() - label("Studio") + 1
  119. end
  120.  
  121. on CalcSpics
  122.   global gSpinLead, gSpinOffset
  123.   set hold to EMPTY
  124.   set count to 0
  125.   repeat with i = 1 to 30
  126.     if the loaded of cast (the number of cast "Studio0001" - 1 + i) = 1 then
  127.       set hold to hold & " " & string(i)
  128.       set count to count + 1
  129.     end if
  130.   end repeat
  131.   return hold & RETURN & " Total = " & string(count) & "  Lead = " & string(gSpinLead) & "  Offset = " & string(gSpinOffset) & "  curr = " & frame() - label("Studio") + 1
  132. end
  133.  
  134. on fKeyEvent
  135.   global gMemorySize, gDirectory, gDebug, gFreeCutOff, gInitialFree
  136.   if gDebug <> -1 then
  137.     set gDebug to gDebug + 1
  138.     if (gDebug = 1) and (the key <> "d") then
  139.       set gDebug to 0
  140.     end if
  141.     if (gDebug = 2) and (the key <> "e") then
  142.       set gDebug to 0
  143.     end if
  144.     if (gDebug = 3) and (the key <> "b") then
  145.       set gDebug to 0
  146.     end if
  147.     if (gDebug = 4) and (the key <> "u") then
  148.       set gDebug to 0
  149.     end if
  150.     if (gDebug = 5) and (the key <> "g") then
  151.       set gDebug to 0
  152.     end if
  153.     if (gDebug = 5) and (the key = "g") then
  154.       set gDebug to -1
  155.       set the text of cast "Display" to " Debugging turned on...  "
  156.       doDisplay(120)
  157.     end if
  158.   else
  159.     if the key = "f" then
  160.       put "ms = " & memorysize() / 1024 & "K   fb = " & freeBytes() / 1024 & "K   used = " & (memorysize() - freeBytes()) / 1024 & "K"
  161.     end if
  162.     if the key = "s" then
  163.       showSpics()
  164.     end if
  165.     if the key = "p" then
  166.       set gDirectory to the pathName
  167.       set the text of cast "Display" to " Directory =  " & the pathName
  168.       doDisplay(60)
  169.     end if
  170.     if the key = "e" then
  171.       set gDirectory to "E:\"
  172.       set the text of cast "Display" to " Directory =  E:\  "
  173.       doDisplay(60)
  174.     end if
  175.     if the key = "d" then
  176.       set gDirectory to "D:\"
  177.       set the text of cast "Display" to " Directory =  D:\  "
  178.       doDisplay(60)
  179.     end if
  180.     if the key = "w" then
  181.       set gDirectory to EMPTY
  182.       set the text of cast "Display" to " Directory =  <empty>  "
  183.       doDisplay(60)
  184.     end if
  185.     if the key = "m" then
  186.       set the text of cast "Display" to " fb = " & freeBytes() / 1024 & "K   used = " & (memorysize() - freeBytes()) / 1024 & "K "
  187.       doDisplay(60)
  188.     end if
  189.     if the key = "n" then
  190.       set the text of cast "Display" to " Memorysize = " & memorysize() / 1024 & "K" & "   Cutoff = " & gFreeCutOff / 1024 & "K"
  191.       doDisplay(60)
  192.     end if
  193.     if the key = "i" then
  194.       set the text of cast "Display" to " iMemorysize = " & gMemorySize / 1024 & "K" & "   ifree = " & gInitialFree / 1024 & "K"
  195.       doDisplay(60)
  196.     end if
  197.     if the key = "b" then
  198.       set the text of cast "Display" to CalcSpics()
  199.       doDisplay(120)
  200.     end if
  201.     if the key = "1" then
  202.       changeLead(1000000)
  203.     end if
  204.     if the key = "2" then
  205.       changeLead(2000000)
  206.     end if
  207.     if the key = "3" then
  208.       changeLead(3000000)
  209.     end if
  210.     if the key = "4" then
  211.       changeLead(4000000)
  212.     end if
  213.     if the key = "5" then
  214.       changeLead(5000000)
  215.     end if
  216.     if the key = "6" then
  217.       changeLead(6000000)
  218.     end if
  219.     if the key = "7" then
  220.       changeLead(7000000)
  221.     end if
  222.     if the key = "8" then
  223.       changeLead(8000000)
  224.     end if
  225.     if the key = "9" then
  226.       changeLead(9000000)
  227.     end if
  228.     if the key = "0" then
  229.       changeLead(10000000)
  230.     end if
  231.   end if
  232. end
  233.  
  234. on doDisplay time
  235.   puppetSprite(48, 1)
  236.   set the castNum of sprite 48 to the number of cast "Display"
  237.   spriteBox(48, 25, 25, 300, 300)
  238.   set the foreColor of sprite 48 to 230
  239.   set the backColor of sprite 48 to 52
  240.   set the visible of sprite 48 to 1
  241.   updateStage()
  242.   set atimer to the ticks
  243.   repeat while (the ticks - atimer) < time
  244.   end repeat
  245.   puppetSprite(48, 0)
  246. end
  247.  
  248. on changeLead mem
  249.   global gSpinLead
  250.   if (frame() >= label("Studio")) and (frame() <= (label("Studio") + 29)) then
  251.     calcSpinLead(mem)
  252.     set the text of cast "Display" to " Lead = " & gSpinLead & "    loading... "
  253.     doDisplay(60)
  254.     reLoadPics()
  255.     set the text of cast "Display" to " Lead = " & gSpinLead & "    loaded!!! "
  256.     doDisplay(60)
  257.   end if
  258. end
  259.  
  260. on installf
  261.   set the keyDownScript to "fKeyEvent"
  262. end
  263.  
  264. on uninstallf
  265.   set the keyDownScript to EMPTY
  266. end
  267.  
  268. on rightTurnDelete
  269.   global gSpinLead, gLastSpinTime, gSpinCount, gSpinOffset
  270.   if gSpinLead < 15 then
  271.     if gSpinOffset = gSpinLead then
  272.       set offset to the castNum of sprite 1 - 1 - (2 * gSpinLead)
  273.       if offset < 0 then
  274.         set offset to 30 + offset
  275.       end if
  276.       unLoadCast(the number of cast "Studio0001" + offset)
  277.     else
  278.       set gSpinOffset to gSpinOffset + 1
  279.     end if
  280.   end if
  281.   set gSpinCount to 1
  282.   set gLastSpinTime to the ticks
  283. end
  284.  
  285. on leftTurnDelete
  286.   global gSpinLead, gLastSpinTime, gSpinCount, gSpinOffset
  287.   if gSpinLead < 15 then
  288.     if gSpinOffset = -gSpinLead then
  289.       set offset to the castNum of sprite 1 - 1 + (2 * gSpinLead)
  290.       if offset > 29 then
  291.         set offset to offset - 30
  292.       end if
  293.       unLoadCast(the number of cast "Studio0001" + offset)
  294.     else
  295.       set gSpinOffset to gSpinOffset - 1
  296.     end if
  297.   end if
  298.   set gLastSpinTime to the ticks
  299.   set gSpinCount to 1
  300. end
  301.  
  302. on preLoadStudioPics
  303.   global gSpinLead, gLastSpinTime, gSpinCount, gSpinWait
  304.   if gSpinWait > 4 then
  305.     set gSpinWait to 1
  306.   else
  307.     set gSpinWait to gSpinWait + 1
  308.   end if
  309. end
  310.  
  311. on ltest index
  312.   set tlist to []
  313.   repeat with i = 1 to 1000
  314.     add(tlist, i)
  315.   end repeat
  316.   set t0 to the ticks
  317.   repeat with i = 1 to 5000
  318.     set hold to getAt(tlist, index)
  319.   end repeat
  320.   put the ticks - t0
  321. end
  322.  
  323. on findCasts start, finish
  324.   set hold to the frame
  325.   set thelist to []
  326.   repeat with f = start to finish
  327.     go(f)
  328.     repeat with S = 1 to 48
  329.       set cn to the castNum of sprite S
  330.       if (cn <> 0) and (getPos(thelist, cn) = 0) then
  331.         add(thelist, cn)
  332.         set the purgePriority of cast cn to 1
  333.       end if
  334.     end repeat
  335.   end repeat
  336.   go(hold)
  337.   return thelist
  338. end
  339.  
  340. on getCasts xcept
  341.   set thelist to []
  342.   repeat with i = 1 to the number of castMembers
  343.     if (the castType of cast i <> #empty) and (the castType of cast i <> #script) and (getPos(xcept, i) = 0) then
  344.       add(thelist, i)
  345.     end if
  346.   end repeat
  347.   return thelist
  348. end
  349.  
  350. on prepareList
  351.   global gOutCasts
  352.   set gOutCasts to getCasts(findCasts(label("Studio"), label("Studio End")))
  353.   put gOutCasts
  354. end
  355.  
  356. on initialiseOutCasts
  357.   global gOutCasts
  358.   set gOutCasts to [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 45, 49, 50, 51, 52, 53, 55, 56, 63, 80, 196, 202, 203, 204, 205, 206, 207, 211, 216, 217, 223, 225, 227, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 245, 246, 247, 249, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 265, 266, 267, 268, 269, 270, 271, 272, 274, 278, 279, 287, 288, 289, 290, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 325, 328, 329, 330, 331, 332, 334, 335, 336, 337, 341, 342, 343, 344, 345, 346]
  359. end
  360.  
  361. on testOutCasts
  362.   global gOutCasts, gOutCastsCount, gOutCastsHold, gSpinWait, gFreeCutOff
  363.   if (gSpinWait = 2) or (gSpinWait = 4) then
  364.     repeat with i = 1 to 20
  365.       if the loaded of cast getAt(gOutCasts, gOutCastsHold) = 1 then
  366.         if freeBytes() < gFreeCutOff then
  367.           unLoadCast(getAt(gOutCasts, gOutCastsHold))
  368.         end if
  369.       end if
  370.       if gOutCastsHold = gOutCastsCount then
  371.         set gOutCastsHold to 1
  372.         next repeat
  373.       end if
  374.       set gOutCastsHold to gOutCastsHold + 1
  375.     end repeat
  376.   end if
  377. end
  378.  
  379. on getpn
  380.   global gDirectory
  381.   return gDirectory
  382. end
  383.  
  384. on LocalStudioFiles
  385.   repeat with i = 1 to 30
  386.     set the fileName of cast i to getpn() & "wstudio\studio" & string(integer(i / 10)) & string(i mod 10) & ".bmp"
  387.   end repeat
  388. end
  389.  
  390. on showFileNames
  391.   repeat with i = 1 to the number of castMembers
  392.     if (the castType of cast i <> #empty) and (the castType of cast i <> #script) and (the fileName of cast i <> EMPTY) then
  393.       put the fileName of cast i
  394.     end if
  395.   end repeat
  396. end
  397.  
  398. on replaceFileNames
  399.   repeat with i = 1 to the number of castMembers
  400.     if (the castType of cast i <> #empty) and (the castType of cast i <> #script) and (the fileName of cast i <> EMPTY) and (the fileName of cast i contains "Movies") then
  401.       set hold to the fileName of cast i
  402.       set hold to the pathName & char offset("Movies", hold) to length(hold) of hold
  403.       set the fileName of cast the name of cast i to hold
  404.     end if
  405.   end repeat
  406. end
  407.  
  408. on setPaths
  409.   global gDirectory
  410.   if "BBB.MOV" = getNthFileNameInFolder(the pathName & "MOVIES", 1) then
  411.     replaceFileNames()
  412.     set gDirectory to the pathName
  413.   else
  414.     if "BBB.MOV" = getNthFileNameInFolder("Jungle Activated! :MOVIES", 1) then
  415.       set gDirectory to "Jungle Activated! :"
  416.       append(the searchPath, "Jungle Activated! :")
  417.     else
  418.       alert("The Jungle Activated CD-ROM has not been found!")
  419.       quit()
  420.     end if
  421.   end if
  422. end
  423.